<!--
 * @Author: zhuhaohua 250293786@qq.com
 * @Date: 2024-01-02 10:21:51
 * @LastEditors: DeluxePig 250293786@qq.com
 * @LastEditTime: 2024-01-13 16:18:05
 * @FilePath: \heart-backed\src\views\banner\index.vue
 * @Description:富文本
-->
<template>
  <div class="quillEditor-box">
    <el-upload
      id="iviewUp"
      ref="upload"
      class="avatar-uploader"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      accept=".jpg,.jpeg,.png,.gif"
      :before-upload="beforeAvatarUpload"
      :action="serverUrl"
      style="display: none;width:0"
    >
      <div style="width: 0">
        <i class="el-icon-plus avatar-uploader-icon" />
      </div>
    </el-upload>
    <quill-editor
      ref="myQuillEditor"
      v-model="content"
      :options="editorOption"
      style="height: 100%;width: 100%;"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @ready="onEditorReady($event)"
      @change="onEditorChange($event)"
    />

  </div>
</template>

<script>
import { quillEditor, Quill } from 'vue-quill-editor'
import { ImageDrop } from 'quill-image-drop-module'
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/imageDrop', ImageDrop)
Quill.register('modules/imageResize', ImageResize)
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],
  ['blockquote', 'code-block'],
  [{ 'list': 'ordered' }, { 'list': 'bullet' }],
  [{ 'script': 'sub' }, { 'script': 'super' }],
  [{ 'indent': '-1' }, { 'indent': '+1' }],
  [{ 'direction': 'rtl' }],
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  [{ 'color': [] }, { 'background': [] }],
  [{ 'font': [] }],
  [{ 'align': [] }],
  ['clean'],
  ['link', 'image']
]
export default {
  components: {
    quillEditor
  },
  props: {
    htmlCont: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      serverUrl: process.env.VUE_APP_BASE_API + '/api/upload',
      content: '<h2>I am Example</h2>',
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              'image': function(value) {
                if (value) {
                  document.querySelector('#iviewUp input').click()
                } else {
                  this.quill.format('image', false)
                }
              }
            }
          },
          history: {
            delay: 1000,
            maxStack: 50,
            userOnly: false
          },
          imageDrop: true,
          imageResize: {
            displayStyles: {
              backgroundColor: 'black',
              border: 'none',
              color: 'white'
            },
            modules: ['Resize', 'DisplaySize', 'Toolbar']
          }
        }
      }
    }
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill
    }
  },
  watch: {
    htmlCont: {
      handler(nv, oldName) {
        if (nv) {
          this.content = nv
        } else {
          this.content = ''
        }
      },
      immediate: true
    }

  },
  created() {
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // res为图片服务器返回的数据
      // 获取富文本组件实例
      const vm = this
      const quill = this.$refs.myQuillEditor.quill
      // console.log('res---', vm.$refs.myQuillEditor.quill.getSelection())
      // 如果上传成功
      if (res.data && res.data.path) {
        // 获取光标所在位置
        const length = quill.getSelection().index
        // 插入图片  res.info为服务器返回的图片地址
        quill.insertEmbed(length, 'image', process.env.VUE_APP_FILE_API + res.data.path)
        // 调整光标到最后
        quill.setSelection(length + 1)
      } else {
        vm.$Message.error('图片插入失败')
      }
      // loading动画消失
      this.quillUpdateImg = false
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
      }
      return isLt2M
    },
    onEditorBlur(quill) {
      // console.log('editor blur!', quill)
    },
    onEditorFocus(quill) {
      // console.log('editor focus!', quill)
    },
    onEditorReady(quill) {
      // console.log('editor ready!', quill)
    },
    onEditorChange({ quill, html, text }) {
      // console.log('quill', quill)
      // console.log('html', html)
      // console.log('text', text)
      this.content = html
      this.$emit('getHtml', html)
    }
  }
}
</script>
<style lang="scss" scoped>
.quillEditor-box{
  width:100%;
  height:360px;
  margin-bottom:100px;
}
</style>
